<template>
    <div>
        <Tables 
        :title ='title' 
        :field = "field" 
        :tableData ='tableData' 
        :butns ="butns" 
        :animateShow = "animate_show"
        :pageSize='pageSize' 
        :totalNum="total_num"  
        :curPage ="cur_page" 
        @loadData ="loadData" 
        @reset = "reset"
		@xmlEvent="exportFile('xml')" 
        @htmlEvent="exportFile('html')" 
        @clearEvent="clearModal" 
        :dbClickDisabled = "true"
        :rankable ="true"
        @changePageSize='changePageSize'
        @rankData = "rankData"
        >
		   <div slot="filter">
				<search :searchFiled="searchFiled" @searchEvent = "searchEvent"></search>
			</div>
		</Tables>
		<Modals 
        :id ="'modal'" 
        :title ="'提示'" 
        :status = "status" 
        :errorMsg="errorMsg" 
		@sentErrorMsg = "getErrorMsg" 
        @modalEvent ="clearAll"
        >
		  	<div slot ="modal_content">
				<p style="font-size:16px;">确定清除所有{{title}}？</p>
			</div>
		  </Modals>
    </div>
</template>
<script>
    import Tables from 'components/common/Tables'
	import search from 'components/common/search'
	import Modals from 'components/common/Modals'
    import logManageService from 'services/logManageService' 
    import PageData from 'libs/PageData'
	
    let butns = [
         {
            type:'other',
            name:'导出XML',
            event:'xmlEvent',
            icon:'/static/img/exportxml.png'
        },
         {
            type:'other',
            name:'导出HTML',
            event:'htmlEvent',
            icon:'/static/img/exporthtml.png'
        },
        {
            type:'other',
            name:'清空',
            event:'clearEvent',
            icon:'/static/img/w_clear.png'
        }
    ]
    let field = [
        {
            type:'rank',
        },
        {
            type:'text',
            label:'等级',
            name:'level',          
        },
		 {
            type:'text',
            label:'事件类型',
            name:'type',          
        },
		{
            type:'text',
            label:'事件内容',
            name:'content',
            ellipsis:'max-width:250px;',        
        },
		{
            type:'text',
            label:'时间',
            name:'time',          
        }   
    ]
	let searchFiled =[
		 {
            type:'select_link',
            label:['等级','事件类型'],
            name:['level','type'],
			meuns:[
				{val:'',label:'全部'},
				{val:'1',label:'普通'},
				{val:'2',label:'警告'},
				{val:'3',label:'严重'}
			]
        },
        {
            type:'time-rang',
            label:'时间范围',
            name:['start_time','end_time'],
        }
	]
    export default {
		components:{
		    Tables,
			Modals,
			search
	    },
        data() {
            return{
                title:'事件日志',
                field:field,
				butns:butns,
                tableData:[],
                pageSize: [10, 20, 40, 80],
                size: 10,
                total_num:1,
                cur_page:1,
                type:'block',
				searchFiled:searchFiled,
				animate_show:false,
				export_data:{},
				status:'',
				errorMsg:'',
                finddata:{},
                orderby: '',
                order: '',
                filter: {
                    level: '',
                    type: '',
                    start_time: '',
                    end_time: ''
                }
            }
        },
        created() {
            this.loadData()
        },
        methods:{
			getErrorMsg(msg) {
                this.errorMsg = msg
            },
			reset(type) {
                this.status = ""
                this.errorMsg = ''
            },  
			exportFile(filetype) {								
				let params = this.export_data
			    params.type = params.type?params.type:''
			    params.level = params.level?params.level:''
				params.starttime = params.starttime?params.starttime:''
				params.endtime = params.endtime?params.endtime:''
				let fileName = '事件日志'+new Date().toLocaleString()
				let setUrl ="/services/v1_1/log/event.php?"
				    +"export="+ fileName + "." + filetype 
				    +"&type=" + params.type
					+"&level=" + params.level 
					+"&start_time=" + params.starttime 
					+"&end_time=" + params.endtime
				window.open(setUrl);
			},
			clearModal() {
				$("#modal").modal('show')
			},
			clearAll() {
				this.status = 'ing'	
				logManageService.clearEventLog()
				.then((res) => {
					
					if (res.errcode === 0) {
						this.status = 'ok'
						let params ={
							page:1,
						}
						this.loadData(params)
					} else {
						this.status = 'error'
						this.errorMsg = res.errorMessage
					}
				})
			},
            loadData() {		
                // if (params.oper=='find') {
                //     this.animate_show = true
				// 	this.finddata = params
				// }
				// if (params.oper=='page_change') {
				// 	params =Object.assign(this.finddata,params) 
				// }
                // this.cur_page = params.page
				// this.export_data = params
                // this.cur_page = params.page
                let params = {
                    t: 10,
                    orderby: this.orderby,
                    order: this.order,
                    page: this.cur_page,
                    row: this.size,
                    level: this.filter.level,
                    type: this.filter.type,
                    start_time: this.filter.start_time,
                    end_time: this.filter.end_time,     
                }
                logManageService.getEventLog(params)
                .then((res) => {
					if (res.errcode === 0) {
						this.animate_show = false       
						this.total_num = res['10'].count
                        this.tableData = res['10'].logs.map((item)=>{
                            switch (item.type) {
                                case 1:
                                    item.type = '网口接入'
                                    break;
                                case 2:
                                    item.type = '网口断开'
                                    break;
                                case 3:
                                    item.type = '登录成功'
                                    break;
                                case 4:
                                    item.type = '用户注销'
                                    break;
                                case 5:
                                    item.type = '系统初始化'
                                    break;
                                case 6:
                                    item.type = '系统启动'
                                    break;
                                case 7:
                                    item.type = '中心连接'
                                    break;
                                 case 8:
                                    item.type = '登录失败'
                                    break;
                                case 9:
                                    item.type = '中心断开'
                                    break;
                                case 10:
                                    item.type = '登录超限'
                                    break;
                                default:
                                    break;
                            }
                            switch (item.level) {
                                case 1:
                                    item.level = '普通'
                                    break;
                                case 2:
                                    item.level = '告警'
                                    break;
                                case 3:
                                    item.level = '严重'
                                    break;
                                default:
                                    break;
                            }
                            return item
                        })
                    }
				})
            },
            changePageSize (page, size) {
                this.cur_page = page
                this.size = size
                this.loadData()
            },
            rankData (param) {
                this.order = param.order
                this.orderby = param.by
                this.loadData()
            },
            searchEvent (params) {
                this.filter.level = params.level ? params.level : ''
                this.filter.type = params.type ? params.type : ''
                this.filter.start_time = params.start_time ? params.start_time : ''
                this.filter.end_time = params.end_time ? params.end_time : ''
                this.cur_page = 1
                this.loadData()
            }   
        }
    }
</script>